/**
 * @filename:PickDetailController Sep 23, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.MoldAllocation;
import com.wonder.app.domain.MoldAndInspect;
import com.wonder.app.domain.MoldMaterial;
import com.wonder.app.domain.PickDetail;
import com.wonder.app.mapper.MoldMaterialDao;
import com.wonder.app.request.MoldAllocationRequest;
import com.wonder.app.request.MoldAndInspectRequest;
import com.wonder.app.request.MoldMaterialRequest;
import com.wonder.app.service.MoldMaterialService;
import com.wonder.app.service.PickDetailService;
import com.wonder.app.request.PickDetailRequest;
import com.wonder.framework.authorization.CheckAdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * <p>说明： 模具列表API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Sep 23, 2024
 *
 */
@Api(tags = "模具列表")
@RestController
@RequestMapping("/app/MoldMaterial")
public class MoldMaterialController {

    @Autowired
    private CheckAdminToken checkAdminToken;
    @Autowired
    private MoldMaterialService moldMaterialService;
    @Autowired
    private MoldMaterialDao moldMaterialDao;
    @Autowired
    private SysAdminService adminService;

    private Map<Long,Long> lastRequestTimeMap = new ConcurrentHashMap<>();

    @ApiOperation(value = "模具物料列表")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Map<String,Object> index(@RequestBody MoldMaterialRequest moldMaterialRequest) {
        Page<MoldMaterial> page = new Page<MoldMaterial>(moldMaterialRequest.getPage(), moldMaterialRequest.getLimit());
        IPage<MoldMaterial> list = moldMaterialService.selectList(moldMaterialRequest, page);

        Map<String,Object> retMap=new HashMap<>();
        retMap.put("data",CommonPage.restPage(list));


        return AjaxMap.success(retMap);
    }
    /**
     * @explain 修改排序开关
     * @param   entity
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/updateExt")
    @ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
    public AjaxResult<MoldMaterial> updateExt(@RequestBody MoldMaterial entity){
        if (null!=entity) {
            boolean rsg =  moldMaterialService.updateById(entity);
            if (rsg) {
                return AjaxResult.success("system.updateSuccess");
            }else {
                return AjaxResult.failed("system.updateFailed");
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }
    /**
     * @explain 查看对象明细
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询prdfeed明细")
    @ResponseBody
    @PostMapping(value = "/moldmaterialdetail")

    public AjaxResult<Map<String,Object>> getmoldmaDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = moldMaterialService.getmoldmaDetail(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 新增模具信息
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "新增模具")
    @ResponseBody
    @PostMapping(value = "/create_mold")
    public AjaxResult<Boolean> createPickBill(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        //从map中查找上次请求的时间戳
        Long lastRequestTime = lastRequestTimeMap.get(userId);
        //如果为空或者上次请求的时间戳与当前时间做差大于5s，则视为新请求，否则重复请求。
        if (lastRequestTime == null || System.currentTimeMillis() - lastRequestTime > 20000) {
            lastRequestTimeMap.put(userId, System.currentTimeMillis());
            // 处理请求
            Boolean res = moldMaterialService.createMoldMaterial(userName,map);
            if (res){
                return AjaxResult.success(res);
            } else {
                return AjaxResult.failed();
            }
        } else {
            // 重复请求
            throw new Exception("入库不要，不要重复点击");
        }


    }

    /**
     * 获取模具信息
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取模具信息")
    @ResponseBody
    @PostMapping(value = "/get_mold")
    public AjaxResult<Map<String,Object>> getMoldMaterial(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = moldMaterialService.getMoldMaterial(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 根据工序计划领用模具
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "根据工序计划领用模具")
    @ResponseBody
    @PostMapping(value = "/pick_mold_by_orderNo")
    public AjaxResult<?> pickMoldByorderNo(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        boolean res = moldMaterialService.pickMoldByorderNo(userName,map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 归还模具
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "根据工序计划归还模具")
    @ResponseBody
    @PostMapping(value = "/return_mold_by_orderNo")
    public AjaxResult<?> ReturnMoldByorderNo(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        boolean res = moldMaterialService.ReturnMoldByorderNo(userName,map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }


    /**
     * 删除领用模具中的临时数据
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "删除领用模具中的临时数据")
    @ResponseBody
    @PostMapping(value = "/delete_pick_mold_temp")
    public AjaxResult<?> deletePickMoldTemp(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        boolean res = moldMaterialService.deletePickMoldTemp(userName,map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 审核领用模具
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "审核领用模具")
    @ResponseBody
    @PostMapping(value = "/audit_pick_mold")
    public AjaxResult<?> auditPickMold(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        boolean res = moldMaterialService.auditPickMold(userName,map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }

}